﻿package labox.innovation.gameserver.skills;

import gnu.trove.TIntObjectHashMap;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastList;
import labox.innovation.config.Config;
import labox.innovation.gameserver.Item;
import labox.innovation.gameserver.datatables.SkillTable;
import labox.innovation.gameserver.model.L2Skill;
import labox.innovation.gameserver.templates.item.L2Armor;
import labox.innovation.gameserver.templates.item.L2EtcItem;
import labox.innovation.gameserver.templates.item.L2EtcItemType;
import labox.innovation.gameserver.templates.item.L2Item;
import labox.innovation.gameserver.templates.item.L2Weapon;

/**
 * @author mkizub
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class SkillsEngine {

	protected static final Logger _log = LoggerFactory.getLogger(SkillsEngine.class.getName());

	private List<File> _armorFiles = new FastList<File>();

	private List<File> _weaponFiles = new FastList<File>();

	private List<File> _etcitemFiles = new FastList<File>();

	private List<File> _skillFiles = new FastList<File>();

	public static SkillsEngine getInstance() {
		return SingletonHolder._instance;
	}

	private SkillsEngine() {
		hashFiles("data/Stats/etcitem", _etcitemFiles);
		hashFiles("data/Stats/armor", _armorFiles);
		hashFiles("data/Stats/weapons", _weaponFiles);
		hashFiles("data/Stats/skills", _skillFiles);
	}

	private void hashFiles(String dirname, List<File> hash) {
		File dir = new File(Config.DATAPACK_ROOT, dirname);

		if (!dir.exists()) {
			_log.info("Dir {}  not exists" , dir.getAbsolutePath());
			return;
		}
		File[] files = dir.listFiles();

		for (File f : files) {

			if (f.getName().endsWith(".xml") && !f.getName().startsWith("custom"))
				hash.add(f);
		}
		File customfile = new File(Config.DATAPACK_ROOT, dirname + "/custom.xml");
		if (customfile.exists())
			hash.add(customfile);
	}

	public List<L2Skill> loadSkills(File file) {
		if (file == null) {
			_log.info("Skill file not found.");
			return null;
		}
		DocumentSkill doc = new DocumentSkill(file);
		doc.parse();
		return doc.getSkills();
	}

	public void loadAllSkills(final TIntObjectHashMap<L2Skill> allSkills) {
		int count = 0;
		for (File file : _skillFiles) {
			List<L2Skill> s = loadSkills(file);
			if (s == null)
				continue;
			for (L2Skill skill : s) {
				allSkills.put(SkillTable.getSkillHashCode(skill), skill);
				count++;
			}
		}
		_log.info("SkillsEngine: Loaded {} Skill templates from XML files." , count );
	}

	public List<L2Armor> loadArmors(Map<Integer, Item> armorData) {
		List<L2Armor> list = new FastList<L2Armor>();
		for (L2Item item : loadData(armorData, _armorFiles, 1)) {
			list.add((L2Armor) item);
		}
		return list;
	}

	public List<L2Weapon> loadWeapons(Map<Integer, Item> weaponData) {
		List<L2Weapon> list = new FastList<L2Weapon>();
		for (L2Item item : loadData(weaponData, _weaponFiles, 2)) {
			list.add((L2Weapon) item);
		}
		return list;
	}

	public List<L2EtcItem> loadItems(Map<Integer, Item> itemData) {
		List<L2EtcItem> list = new FastList<L2EtcItem>();
		List<Integer> xmlItem = new FastList<Integer>();

		for (L2Item item : loadData(itemData, _etcitemFiles, 3)) {
			list.add((L2EtcItem) item);
			xmlItem.add(item.getItemId());
		}
		for (Item item : itemData.values()) {
			if (!xmlItem.contains(item.id))
				list.add(new L2EtcItem((L2EtcItemType) item.type, item.set));
		}
		return list;
	}

	/**
	 * 
	 * @param itemData
	 * @param files
	 * @param iDentify
	 *            1:盔甲 2:武器 3:其他道具物品
	 * @return
	 */
	public List<L2Item> loadData(Map<Integer, Item> itemData, List<File> files, int iDentify) {
		List<L2Item> list = new FastList<L2Item>();
		for (File f : files) {
			DocumentItem document = new DocumentItem(itemData, f, iDentify);
			document.parse();
			list.addAll(document.getItemList());
		}
		return list;
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final SkillsEngine _instance = new SkillsEngine();
	}
}
